const _ = require('lodash');

function getArea (r) {
  console.log(r);
  return Math.PI * r * r;
}

let getAreaWithMemory =_.memoize(getArea);
console.log(getAreaWithMemory(4)); // 会打印r
console.log(getAreaWithMemory(4)); // 不会打印r

// 模拟 memoize 方法的实现
function memoize (fn) {
  let cache = {};
  return function () {
    let key = JSON.stringify(arguments);
    cache[key] = cache[key] || fn.apply(fn, arguments);
    return cache[key];
  }
}

let getAreaWithMemory2 = memoize(getArea);
console.log(getAreaWithMemory2(4)); // 会打印r
console.log(getAreaWithMemory2(4)); // 不会打印r